package com.aceitunaproject.server.message;

import java.io.Serializable;
import java.nio.ByteBuffer;

import com.aceitunaproject.server.player.Player;
import com.aceitunaproject.shared.message.Message;
import com.aceitunaproject.shared.message.getUnit.GetUnitResponse;
import com.aceitunaproject.shared.message.hello.HelloWorldRequest;
import com.aceitunaproject.shared.message.hello.HelloWorldResponse;
import com.aceitunaproject.shared.unit.Unit;

/**
 * Default message handler implementation
 * 
 * @author Maciej Miechowicz
 */
public class DefaultMessageHandler implements MessageHandler, Serializable {

	private static final long serialVersionUID = 1209906537186889109L;

	/**
	 * {@inheritDoc}
	 */
	public void handleBinaryMessage(Player player, ByteBuffer message) {
		if(message == null)
			throw new NullPointerException("Null message");
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void handleObjectMessage(Player player, Message message) {
		if(message == null)
			throw new NullPointerException("Null message");
		
		String messageType = message.getMessageType();
		if (messageType == "HelloWorldRequest") {
			handleHelloWorld(player, message);
		}
		else if (messageType == "GetUnitRequest") {
			handleGetUnit(player, message);
		}
	}
	
	/**
	 * Handles get unit request
	 * 
	 * @param player player the message was received from
	 * @param message received message
	 */
	private void handleGetUnit(Player player, Message message) {
		//for now only returns an empty unit
		player.sendObjectMessage(new GetUnitResponse(new Unit()));
		
	}

	/**
	 * Handles hello world request
	 * 
	 * @param player player the message was received from
	 * @param message received message
	 */
	private void handleHelloWorld(Player player, Message message) {
		player.sendObjectMessage(new HelloWorldResponse("Hello world"));
	}
	
}
